//app.js
require('mixin/mixin.js')
import weRequest from 'utils/weRequest.js';
App({
  onLaunch: function () {
    console.log("onLaunch")
    this.checkUpdateVersion();// 检查更新
    // this.getWXuserInfo();// 获取用户信息
    // this.getADByminiappinfoid();// 广告信息
    this.getEAlistByNo();// 控制页面部分显隐
    // this.getMiniappInfo();// 微信小程序信息->当前AI纠音模型，及小程序名称
    this.setPageTabBarBadge();
    // this.setMenuButtonInfo();
  },

  // 检查版本更新
  checkUpdateVersion: function () {
    var that = this
    //判断微信版本是否 兼容小程序更新机制API的使用
    if (wx.canIUse('getUpdateManager')) {
      //创建 UpdateManager 实例
      const updateManager = wx.getUpdateManager();
      console.log('是否进入模拟更新');
      //检测版本更新
      updateManager.onCheckForUpdate(function (res) {
        console.log('是否获取版本');
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          //监听小程序有版本更新事件
          updateManager.onUpdateReady(function () {
            //TODO 新的版本已经下载好，调用 applyUpdate 应用新版本并重启 （ 此处进行了自动更新操作）
            updateManager.applyUpdate();
            wx.showToast({
              title: '自动更新成功',
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新版本下载失败
            wx.showModal({
              title: '已经有新版本喽~',
              content: '请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开哦~',
            })
          })
        }
      })
    } else {
      //TODO 此时微信版本太低（一般而言版本都是支持的）
      wx.showModal({
        title: '溫馨提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

   //获取手机屏幕的高度
  getappsysteminfo: function (cb) {
    wx.getSystemInfo({
      success: function (res) {
        typeof cb == "function" && cb(res);
      }
    })
  },

  //  页面隐藏控制
  getEAlistByNo: function () {
    var that = this;
    weRequest.request({
      url: 'ea/getEAByminiappid',
      showLoading: false,
      method: 'get',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: { no: '004' }
    }).then((res) => {
      if (res.code == '200') {
        if (that.callback && typeof that.callback == 'function') {
          that.callback(res.data);
        }
        that.globalData.evadingAudit = res.data;
      }
    })
  },

  // 查询信息条数、签到信息、用户角色、签到相差天数
  setPageTabBarBadge: function () {
    var that = this;
    weRequest.request({
      url: 'signIn/initMinAppInfo',
      showLoading: false,
      method: 'get',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {}
    }).then((res) => {
      that.globalData.userInfo.userType = res.data.userRole;
      that.globalData.userInfo.msgNum = res.data.msgNum;
      that.globalData.userInfo.userObj = res.data.hasOwnProperty('userObj') == true ? res.data.userObj : null;
      that.globalData.userInfo.signInfo = res.data.signInfo;
      that.globalData.userInfo.integralObj = res.data.integralObj;
      that.globalData.userInfo.dayDiffNum = res.data.dayDiffNum;
      if (res.data.msgNum > 0) {
        wx.setTabBarBadge({
          index: 2,
          text: res.data.msgNum + "",
          success: function () { }
        })
      }
      if (that.callbackSignIntegralInfo && typeof that.callbackSignIntegralInfo == 'function') {
        that.callbackSignIntegralInfo(that.globalData);
      }
    })
  },

  //  页面广告
  getADByminiappinfoid: function () {
    var that = this;
    weRequest.request({
      url: 'advertisement/getADByminiappinfoid',
      showLoading: false,
      method: 'get',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {}
    }).then((res) => {
      if (res.code == '200') {
        if (that.callback4AD && typeof that.callback4AD == 'function') {
          that.callback4AD(res.data);
        }
        that.globalData.advertisement = res.data;
      }
    })

  },

  // 查询用户头像昵称等微信信息
  getWXuserInfo(){    
    var that = this;
    weRequest.request({
      url: 'wxopenInfo/hasExitWXInfo',
      showLoading: false,
      method: 'get',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {}
    }).then((res) => {
      if (res.code == '200') {
        if (that.callback4WXuserInfo && typeof that.callback4WXuserInfo == 'function') {
          that.callback4WXuserInfo(res.data);
        }
        that.globalData.wxUserInfo = res.data;
      }
    })
  },

   // 查询微信小程序信息-> 当前AI纠音模型，及小程序名称，積分規則
   getMiniappInfo(){    
    var that = this;
    weRequest.request({
      url: 'miniapp/getMiniappInfo',
      showLoading: false,
      method: 'get',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {}
    }).then((res) => {
      if (res.code == '200') {
        if (that.callback4MiniappInfo && typeof that.callback4MiniappInfo == 'function') {
          that.callback4MiniappInfo(res.data);
        }
        that.globalData.miniappInfo = res.data;
      }
    })
  },

  globalData: {
    appPageInfo: {},
    teacherInfo: {},
    evadingAudit: {},
    advertisement: [],
    stuInfo: {},
    userInfo: {},
    wxUserInfo:{},//用户微信信息，头像昵称等
    miniappInfo:{}// 包含信息:小程序名称及当前纠音AI模型
  },
})